<template>
	<xn-form-container
		:title="formData.id ? '编辑选手表' : '增加选手表'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="数字ID：" name="palyId">
				<a-input v-model:value="formData.palyId" placeholder="请输入数字ID" allow-clear />
			</a-form-item>
			<a-form-item label="昵称：" name="nickname">
				<a-input v-model:value="formData.nickname" placeholder="请输入昵称,最好是大家都熟悉的名字" allow-clear />
			</a-form-item>
			<a-form-item label="MMR：" name="mmr">
				<a-input-number v-model:value="formData.mmr" placeholder="请输入MMR" :min="1" :max="20000" style="width: 100%" />
			</a-form-item>
			
			<a-form-item label="能力值：" name="ability">
				<a-slider v-model:value="formData.ability" placeholder="请滑动能力值" :max="100" style="width: 100%" />
			</a-form-item>
			<a-form-item label="擅长位置：" name="positions">
				<a-select
					v-model:value="formData.positions"
					mode="multiple"
					placeholder="请选择擅长位置"
					:options="playerPositionOptions"
					allow-clear
					:max-tag-count="3"
				/>
			</a-form-item>
			<a-form-item label="词条属性：" name="tags">
				<a-textarea v-model:value="formData.tags" placeholder="请输入词条属性" :auto-size="{ minRows: 3, maxRows: 5 }" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="playerForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import playerApi from '@/api/dota/playerApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const defaultFormData = () => ({
		positions: []
	})
	const formData = ref(defaultFormData())
	const submitLoading = ref(false)
	const playerPositionOptions = ref([])

	const normalizePositions = (value) => {
		if (!value) {
			return []
		}
		if (Array.isArray(value)) {
			return value
		}
		return String(value)
			.split(',')
			.map((item) => item.trim())
			.filter((item) => item)
	}

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		playerPositionOptions.value = tool.dictList('player_position')
		if (record) {
			let recordData = cloneDeep(record)
			const positions = normalizePositions(recordData.positions || recordData.position || recordData.goodPositions)
			formData.value = Object.assign({}, defaultFormData(), recordData, {
				positions
			})
		} else {
			formData.value = defaultFormData()
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value?.resetFields()
		formData.value = defaultFormData()
		open.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				if (Array.isArray(formDataParam.positions)) {
					formDataParam.positions = formDataParam.positions.join(',')
				}
				// 兼容可能的字段命名
				formDataParam.position = formDataParam.positions
				formDataParam.goodPositions = formDataParam.positions
				playerApi
					.playerSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
